// WndSpeedmeter.cpp : implementation file
//

#include "stdafx.h"
#include "BBViewer.h"
#include "WndSpeedmeter.h"


// CWndSpeedmeter
CWndSpeedmeter* g_pSpeedmeter = NULL;

IMPLEMENT_DYNAMIC(CWndSpeedmeter, CWnd)

CWndSpeedmeter::CWndSpeedmeter()
{
	g_pSpeedmeter	= this;
	m_nSpeed		= 0;
	m_dwViewType	= 0;
	m_fSpeed = 0;

}

CWndSpeedmeter::~CWndSpeedmeter()
{
	g_pSpeedmeter = NULL;
}


BEGIN_MESSAGE_MAP(CWndSpeedmeter, CWnd)
	ON_WM_PAINT()
	ON_WM_CREATE()
END_MESSAGE_MAP()



// CWndSpeedmeter message handlers

int CWndSpeedmeter::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	LPST_WND_POS lpWndPos = theApp.WndPos.GetAt(wpSpeedometer);
	m_nMode = lpWndPos->nMode;

	return 0;
}

void CWndSpeedmeter::OnPaint()
{
	CPaintDC dc(this);
	UpdateSpeed();

}

void CWndSpeedmeter::SetSpeed(FLOAT fSpeed)
{
	if (m_dwViewType == 0)
		m_fSpeed = (FLOAT)(fSpeed*1.852);
	else
		m_fSpeed = (FLOAT)(fSpeed*1.150);
	UpdateSpeed();
}

void CWndSpeedmeter::SetViewType(DWORD dwType)
{
	m_dwViewType = dwType;
	UpdateSpeed();
}

void CWndSpeedmeter::UpdateSpeed()
{
	CDC* pDC = GetDC();
	CDC dcMemory;
	CDC dcMemory2;

	dcMemory.CreateCompatibleDC(pDC);
	dcMemory2.CreateCompatibleDC(pDC);

	CBitmap *pBmpBackOld, bmpBack;
	BITMAP bmBack;
	CBitmap *pBmp, *pBmpOld;
	LPST_BMP_INFO lpBmpInfo;
	LPST_WND_POS lpWndPos = theApp.WndPos.GetAt(wpSpeedometer);
	lpBmpInfo = theApp.BmpInfo.GetAt(biSpeedometer_bk);
	pBmp = CBitmap::FromHandle(lpBmpInfo->hbmpImage);
	pBmpOld = dcMemory2.SelectObject(pBmp);
	if (pBmp)
	{
		pBmp->GetObject(sizeof(bmBack), &bmBack);

		bmpBack.CreateBitmapIndirect(&bmBack);
		pBmpBackOld = dcMemory.SelectObject(&bmpBack);
	}
	else
	{
		bmpBack.CreateCompatibleBitmap(pDC, lpBmpInfo->rtImageRect.Width(), lpBmpInfo->rtImageRect.Height());
		pBmpBackOld = dcMemory.SelectObject(&bmpBack);
	}

	dcMemory.BitBlt(0, 0, lpBmpInfo->rtImageRect.Width(), lpBmpInfo->rtImageRect.Height(),
					&dcMemory2, 0, 0, SRCCOPY);
	dcMemory2.SelectObject(pBmpOld);

	CFont Font1, Font2, *oldFont;
	int nBottom, nLeft;
	LOGFONT lf;

	lf = g_FontSpeedmeter1;
	if (m_nMode == 0)
	{
		nBottom = 45;
		nLeft = 10;
	}
	else if (m_nMode == 1)
	{
		lf.lfHeight = 26;
		nBottom = lpWndPos->rtWndPos.Height()/2+12;
		nLeft = 8;
	}

	Font1.CreateFontIndirect(&lf);
	oldFont = dcMemory.SelectObject(&Font1); 
	dcMemory.SetBkMode(TRANSPARENT);
	dcMemory.SetTextColor(lpWndPos->dwColorText);
	

	TCHAR buff[16];
	wsprintf(buff, _T("%03d"), (int)(m_fSpeed));
	dcMemory.SetTextAlign(TA_BOTTOM);
	dcMemory.TextOut(nLeft, nBottom, buff);

	lf = g_FontSpeedmeter2;
	if (m_nMode == 0)
	{
		nLeft = 65;
	}
	else if (m_nMode == 1)
	{
		lf.lfHeight = 14;
		nLeft = 55;
	}

	Font2.CreateFontIndirect(&lf);
	dcMemory.SelectObject(oldFont);
	oldFont = dcMemory.SelectObject(&Font2); 
	dcMemory.SetBkMode(TRANSPARENT);
	dcMemory.SetTextColor(lpWndPos->dwColorText);

	if (m_dwViewType == 0)
		dcMemory.TextOut(nLeft, nBottom, _T("Km/h"));
	else
		dcMemory.TextOut(nLeft, nBottom, _T("MPH"));

	pDC->BitBlt(0, 0, lpBmpInfo->rtImageRect.Width(), lpBmpInfo->rtImageRect.Height(), &dcMemory, 0, 0, SRCCOPY);

	Font1.DeleteObject();
	Font2.DeleteObject();

	dcMemory.SelectObject(oldFont);
	dcMemory.SelectObject(pBmpBackOld);	
	dcMemory2.DeleteDC();		//2013.04.29

	ReleaseDC(pDC);
}

